package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.PartyProbationary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PartyActivist;
import com.ruoyi.system.service.IPartyActivistService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入党积极分子申请Controller
 * 
 * @author ruoyi
 * @date 2025-10-16
 */
@RestController
@RequestMapping("/system/activist")
public class PartyActivistController extends BaseController
{
    @Autowired
    private IPartyActivistService partyActivistService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询入党积极分子申请列表
     */
    //("@ss.hasPermi('system:activist:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyActivist partyActivist)
    {
        startPage();
        List<PartyActivist> list = partyActivistService.selectPartyActivistList(partyActivist);
        return getDataTable(list);
    }

    /**
     * 导出入党积极分子申请列表
     */
    //("@ss.hasPermi('system:activist:export')")
    @Log(title = "入党积极分子申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyActivist partyActivist)
    {
        List<PartyActivist> list = partyActivistService.selectPartyActivistList(partyActivist);
        ExcelUtil<PartyActivist> util = new ExcelUtil<PartyActivist>(PartyActivist.class);
        util.exportExcel(response, list, "入党积极分子申请数据");
    }

    /**
     * 获取入党积极分子申请详细信息
     */
    //("@ss.hasPermi('system:activist:query')")
    @GetMapping(value = "/{activistId}")
    public AjaxResult getInfo(@PathVariable("activistId") Long activistId)
    {
        return success(partyActivistService.selectPartyActivistByActivistId(activistId));
    }

    /**
     * 新增入党积极分子申请
     */
    //("@ss.hasPermi('system:activist:add')")
    @Log(title = "入党积极分子申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyActivist partyActivist)
    {

        return toAjax(partyActivistService.insertPartyActivist(partyActivist));
    }

    /**
     * 修改入党积极分子申请
     */
    //("@ss.hasPermi('system:activist:edit')")
    @Log(title = "入党积极分子申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyActivist partyActivist)
    {
        return toAjax(partyActivistService.updatePartyActivist(partyActivist));
    }

    /**
     * 审核入党积极分子申请
     */
    //("@ss.hasPermi('system:activist:edit')")
    @Log(title = "入党积极分子申请审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody PartyActivist partyActivist)
    {
        partyActivist.setAuditTime(new Date());
        String auditStatus = partyActivist.getAuditStatus();
        PartyActivist que = partyActivistService.selectPartyActivistByActivistId(partyActivist.getActivistId());
        // 如果审核通过，更新用户的当前阶段为入党积极分子
        if ("1".equals(auditStatus)) {
            partyActivist.setApprovalDate(new Date());
            sysUserService.updateUserCurrentStage(que.getUserId(), "1");
        }
        
        return toAjax(partyActivistService.updatePartyActivist(partyActivist));
    }

    /**
     * 删除入党积极分子申请
     */
    //("@ss.hasPermi('system:activist:remove')")
    @Log(title = "入党积极分子申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activistIds}")
    public AjaxResult remove(@PathVariable Long[] activistIds)
    {
        return toAjax(partyActivistService.deletePartyActivistByActivistIds(activistIds));
    }
}
